package cn.studyjams.s1.sj64.phonehomeownership;

import android.animation.PropertyValuesHolder;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.view.menu.ActionMenuItemView;
import android.util.Xml;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import cn.studyjams.s1.sj64.phonehomeownership.domain.PhoneInfo;

public class MainActivity extends AppCompatActivity {
    private final static int SUCCESS = 1;
    private final static int FAIL = 0;
    private EditText et_phone;
    private TextView tv_content;
    private Button bt_inquiry;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SUCCESS:
                    PhoneInfo phoneInfo = (PhoneInfo) msg.obj;
                    tv_content.setText(phoneInfo.toString());
                    Toast.makeText(MainActivity.this, "查询成功", Toast.LENGTH_SHORT).show();
                    break;
                case FAIL:
                    Toast.makeText(MainActivity.this, (String)msg.obj, Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et_phone = (EditText) findViewById(R.id.et_phone);
        bt_inquiry = (Button) findViewById(R.id.bt_inquiry);
        tv_content = (TextView) findViewById(R.id.tv_content);

        bt_inquiry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(){
                    @Override
                    public void run() {
                        Message mes = Message.obtain();
                        String phone = et_phone.getText().toString();
                        PhoneInfo phoneInfo = new PhoneInfo(phone);
                        String str = getResources().getString(R.string.api)+phone;
                        try {
                            URL url = new URL(str);
                            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                            conn.setRequestMethod("GET");
                            int code;
                            try{
                                code = conn.getResponseCode();

                            }catch (IOException e){
                                code = 404;
                            }
                            if (code != 200){
                                mes.what= FAIL;
                                mes.obj="网络连接异常，错误代码:"+code;
                                handler.sendMessage(mes);
                                return;

                            }
                            InputStream in = conn.getInputStream();
                            XmlPullParser parser = Xml.newPullParser();
                            parser.setInput(in,"gb2312");
                            int type = parser.getEventType();
                            while (type!=XmlPullParser.END_DOCUMENT){
                                if (type == XmlPullParser.START_TAG) {
                                    String name = parser.getName();
                                    if ("retmsg".equals(name)&&!parser.nextText().equals("OK")){

                                        mes.what=FAIL;
                                        mes.obj="号码不符合";
                                        handler.sendMessage(mes);
                                        return;
                                    }else if ("city".equals(name)){
                                        phoneInfo.setCity(parser.nextText());
                                    }else if ("province".equals(name)){
                                        phoneInfo.setProvince(parser.nextText());
                                    }else if ("supplier".equals(name)){
                                        phoneInfo.setSupplier(parser.nextText());
                                    }
                                }
                                type = parser.next();

                            }
                            mes.what=SUCCESS;
                            mes.obj=phoneInfo;
                            handler.sendMessage(mes);
                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (XmlPullParserException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();


            }
        });
    }
}
